﻿namespace ZQNB.Common.Encrypt
{
    /// <summary>
    /// 助手类的工厂方法类，取代了volatile的双锁模式的单例模式
    /// </summary>
    public static class MySecurityFactory
    {
        private static readonly object _hashHelperLock = new object();
        //哈希
        private static MyHashHelper _hashHelper = null;
        public static MyHashHelper HashHelper
        {
            get
            {
                if (_hashHelper == null)
                {
                    lock (_hashHelperLock)
                    {
                        if (_hashHelper == null)
                        {
                            _hashHelper = new MyHashHelper();
                        }
                    }
                }
                return _hashHelper;
            }
        }

        //随即
        private static readonly object _randomHelperLock = new object();
        private static MyRandomHelper _randomHelper = null;
        public static MyRandomHelper RandomHelper
        {
            get
            {
                if (_randomHelper == null)
                {
                    lock (_randomHelperLock)
                    {
                        if (_randomHelper == null)
                        {
                            _randomHelper = new MyRandomHelper();
                        }
                    }
                }
                return _randomHelper;
            }
        }

        //签名
        public static MySignatureHelper SignatureHelper
        {
            get
            {
                return MySignatureHelper.Instance;
            }
        }

        private static readonly object _asymmetricCryptoHelperLock = new object();
        private static MyAsymmetricCryptoHelper _asymmetricCryptoHelper = null;
        //非对称
        public static MyAsymmetricCryptoHelper AsymmetricCryptoHelper
        {
            get
            {
                if (_asymmetricCryptoHelper == null)
                {
                    lock (_asymmetricCryptoHelperLock)
                    {
                        if (_asymmetricCryptoHelper == null)
                        {
                            _asymmetricCryptoHelper = new MyAsymmetricCryptoHelper();
                        }
                    }
                }
                return _asymmetricCryptoHelper;
            }
        }

        private static readonly object _symmetricCryptoHelperLock = new object();
        private static MySymmetricCryptoHelper _symmetricCryptoHelper = null;
        //对称
        public static MySymmetricCryptoHelper SymmetricCryptoHelper
        {
            get
            {
                if (_symmetricCryptoHelper == null)
                {
                    lock (_symmetricCryptoHelperLock)
                    {
                        if (_symmetricCryptoHelper == null)
                        {
                            _symmetricCryptoHelper = new MySymmetricCryptoHelper();
                        }
                    }
                }
                return _symmetricCryptoHelper;
            }
        }

        //====ZQNB=====
        //ZQNB非对称
        public static MyAsymmetricCryptoHelper CreateAsymmetricCryptoHelper(string container)
        {
            MyAsymmetricCryptoHelper instance = new MyAsymmetricCryptoHelper(container);
            return instance;
        }
        public static MyAsymmetricCryptoHelper CreateAsymmetricCryptoHelper()
        {
            return CreateAsymmetricCryptoHelper(null);
        }

        private static readonly object _zQNBAsymmetricCryptoHelperInstanceLock = new object();
        private static MyAsymmetricCryptoHelper _zQNBAsymmetricCryptoHelperInstance = null;
        public static MyAsymmetricCryptoHelper ZQNBAsymmetricCryptoHelper
        {
            get
            {
                if (_zQNBAsymmetricCryptoHelperInstance == null)
                {
                    lock (_zQNBAsymmetricCryptoHelperInstanceLock)
                    {
                        if (_zQNBAsymmetricCryptoHelperInstance == null)
                        {
                            _zQNBAsymmetricCryptoHelperInstance = CreateAsymmetricCryptoHelper();
                        }
                    }
                }
                return _zQNBAsymmetricCryptoHelperInstance;
            }
        }
        //ZQNB对称
        public static MySymmetricCryptoHelper CreateMySymmetricCryptoHelper(string password)
        {
            string iv = "zqnbzqnb";
            string temp = password;
            string tempFix = "3gi8JoG&D0g=";
            if (string.IsNullOrEmpty(password))
            {
                temp = ")(*&";
            }
            else if (password.Length < 4)
            {
                for (int i = 0; i < 4 - password.Length; i++)
                {
                    temp += i.ToString();
                }
            }
            else if (password.Length > 4)
            {
                temp = password.Substring(0, 4);
            }
            temp += tempFix;
            MySymmetricCryptoHelper instance = new MySymmetricCryptoHelper(iv, temp);
            return instance;
        }
        public static MySymmetricCryptoHelper CreateMySymmetricCryptoHelper()
        {
            return CreateMySymmetricCryptoHelper(null);
        }

        private static readonly object _zQNBSymmetricCryptoHelperInstanceLock = new object();
        private static MySymmetricCryptoHelper _zQNBSymmetricCryptoHelperInstance = null;
        public static MySymmetricCryptoHelper ZQNBSymmetricCryptoHelper
        {
            get
            {
                if (_zQNBSymmetricCryptoHelperInstance == null)
                {
                    lock (_zQNBSymmetricCryptoHelperInstanceLock)
                    {
                        if (_zQNBSymmetricCryptoHelperInstance == null)
                        {
                            _zQNBSymmetricCryptoHelperInstance = CreateMySymmetricCryptoHelper();
                        }
                    }
                }
                return _zQNBSymmetricCryptoHelperInstance;
            }
        }
    }
}
